package com.exposit.books.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.exposit.books.domain.Order;
import com.exposit.books.service.OrderService;

@Controller
@RequestMapping("/admin")
public class AdminViewOrdersController {

	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/avieworders", method = RequestMethod.GET)
	public String avieworders(Model model) {
		return "administrator/vieworders/avieworders";
	}

	@RequestMapping(value = "/aviewallorders", method = RequestMethod.GET)
	public String aviewallorders(Model model) {
		model.addAttribute("allOrders", orderService.getAll());
		return "administrator/vieworders/aviewallorders";
	}

	@RequestMapping(value = "/aviewsuccesfullorders", method = RequestMethod.GET)
	public String aviewsuccesfullorders(Model model) {

		List<Order> listOrder = orderService.getAll();
		List<Order> listSuccesfullOrder = new ArrayList<Order>();
		for (int i = 0; i < listOrder.size(); i++) {
			if (listOrder.get(i).getStatus() == 0) {
				listSuccesfullOrder.add(listOrder.get(i));
			}
		}
		model.addAttribute("succesfullOrders", listSuccesfullOrder);
		return "administrator/vieworders/aviewsuccesfullorders";
	}

	@RequestMapping(value = "/aviewpartlyorders", method = RequestMethod.GET)
	public String aviewpartlyorders(Model model) {

		List<Order> listOrder = orderService.getAll();
		List<Order> listPartlyOrder = new ArrayList<Order>();
		for (int i = 0; i < listOrder.size(); i++) {
			if (listOrder.get(i).getStatus() == -1) {
				listPartlyOrder.add(listOrder.get(i));
			}
		}
		model.addAttribute("partlyOrders", listPartlyOrder);
		return "administrator/vieworders/aviewpartlyorders";
	}

	@RequestMapping(value = "/aviewneworders", method = RequestMethod.GET)
	public String aviewneworders(Model model) {

		List<Order> listOrder = orderService.getAll();
		List<Order> listNewOrder = new ArrayList<Order>();
		for (int i = 0; i < listOrder.size(); i++) {
			if (listOrder.get(i).getStatus() == 1) {
				listNewOrder.add(listOrder.get(i));
			}
		}
		model.addAttribute("newOrders", listNewOrder);
		return "administrator/vieworders/aviewneworders";
	}
	
	@RequestMapping(value = "/aviewinpublishorders", method = RequestMethod.GET)
	public String aviewinpublishorders(Model model) {

		List<Order> listOrder = orderService.getAll();
		List<Order> listPublishOrders = new ArrayList<Order>();
		for (int i = 0; i < listOrder.size(); i++) {
			if (listOrder.get(i).getStatus() == -2) {
				listPublishOrders.add(listOrder.get(i));
			}
		}
		model.addAttribute("publishOrders", listPublishOrders);
		return "administrator/vieworders/aviewinpublishorders";
	}
}
